package com.qfedu.edu.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.qfedu.edu.mapper.UserMapper;
import com.qfedu.edu.pojo.User;
import com.qfedu.edu.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author xiaobobo
 * @title: UserService
 * @projectName CD-Java-FY-2401-Springboot-Parent
 * @description: TODO
 * @date 2024/9/10  14:50
 */
@Service
public class UserService implements IUserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public List<User> findUserAll() {
        return userMapper.list();
    }

    @Override
    public void addUser(User user) {
        if(null==user|| StringUtils.isEmpty(user.getUsername())||StringUtils.isEmpty(user.getPassword())){
          throw new RuntimeException("参数有误");
        }
        //执行到这里参数无误
        userMapper.insert(user);
    }

    @Override
    public void deleteById(Integer userId) {
        if(null==userId){
            throw new RuntimeException("参数有误");
        }
        //执行到这里参数是对的
        userMapper.deleteById(userId);
    }

    @Override
    public User findUserById(Integer userId) {
        if(null==userId){
            throw new RuntimeException("参数有误");
        }
        //执行到这里 参数无误
        User user=userMapper.listById(userId);
        return user;
    }

    @Override
    public void updateUser(User user) {
        if(null==user|| StringUtils.isEmpty(user.getUsername())||StringUtils.isEmpty(user.getPassword())||null==user.getUserId()){
            throw new RuntimeException("参数有误");
        }
        //执行到这里参数无误
        userMapper.update(user);
    }
}
